//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.tedu.bonfire.front.content.controller;

import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.web.JsonResult;
import cn.tedu.bonfire.front.content.pojo.dto.AddNewTagDTO;
import cn.tedu.bonfire.front.content.pojo.vo.TagListStartVO;
import cn.tedu.bonfire.front.content.service.ITagService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping({"tag"})
@Api(tags = {"4. 标签管理"})
@Validated
public class TagController {
    private static final Logger log = LoggerFactory.getLogger(TagController.class);
    @Autowired
    private ITagService tagService;

    public TagController() {
    }

    @PostMapping({"add-new"})
    @ApiOperation("新增标签")
    @ApiOperationSupport(
            order = 100
    )
    public JsonResult addNew(AddNewTagDTO addNewTagDTO) {
        tagService.annNew(addNewTagDTO);
        return JsonResult.ok();
    }

    @GetMapping({""})
    @ApiOperation("标签列表")
    @ApiOperationSupport(
            order = 400
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "page",
            value = "页码",
            defaultValue = "1",
            paramType = "query",
            dataType = "int"
    )})
    public JsonResult list(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal, Integer page) {
        Integer pageNum = page == null ? 1 : page;
        PageData<TagListStartVO> pageData = tagService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping({"/rebuild-search"})
    @ApiOperation("重建标签的搜索数据")
    @ApiOperationSupport(
            order = 500
    )
    public JsonResult listSearch() {
        tagService.rebuildSearch();
        return JsonResult.ok();
    }

    @GetMapping({"/search"})
    @ApiOperation("搜索标签")
    @ApiOperationSupport(
            order = 510
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "keyword",
            value = "关键词",
            paramType = "query",
            dataType = "string"
    ), @ApiImplicitParam(
            name = "page",
            value = "页码",
            paramType = "query",
            dataType = "int"
    )})
    public JsonResult search(String keyword, Integer page) {

        PageData<TagListStartVO> searchHits = tagService.search(keyword, page);
        return JsonResult.ok(searchHits);
    }
}
